package com.koron.css2.clientCenter.impl;

import com.google.common.collect.Maps;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.bean.ValveCloseDetailBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.mapper.ValveCloseDetailMapper;
import com.koron.css2.clientCenter.mapper.ValveUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.util.CollectionUtils;
import org.swan.bean.MessageBean;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 开阀
 *
 * @author 杨嘉慧
 */
public class ValveOpen implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ValveUserMapper userMapper = factory.getMapper(ValveUserMapper.class);
            ValveCloseDetailBean detailBean = JsonUtils.objectToPojo(req.getData(), ValveCloseDetailBean.class);
            String valveStatus = userMapper.getValveStatus(detailBean.getUserNo());

            UserInfoMapper useInfoMapper = factory.getMapper(UserInfoMapper.class);
            UserBean beforeUser = useInfoMapper.getUserInfoByUserNo(detailBean.getUserNo(), 1);
            beforeUser.setValveStatus(Tools.getDicNameByCodeAndValue(factory,"MVS",valveStatus));

            //阀门状态不是开阀才能开阀
            if (null == valveStatus || (!"2".equals(valveStatus) && !"3".equals(valveStatus))) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "开阀失败,阀门状态不是阀关/半开", null);
            }
            ValveCloseDetailMapper detailMapper = factory.getMapper(ValveCloseDetailMapper.class);
            //更新用户表，水表表阀门状态为阀开
            userMapper.openValve(detailBean.getUserNo());
            //更新开阀单数据
            detailMapper.updateOpenBill(detailBean.getId(), userInfo.getUserInfo().getAcount(),
                    userInfo.getUserInfo().getName());

            //用户变更事项记录
            List<UserChangeItemBean> item = new ArrayList<UserChangeItemBean>();
            item.addAll(userChangeItems(factory,detailBean,userInfo, beforeUser));
            //添加用户变更事项记录
            ChangeUserMapper changeUserMapper = factory.getMapper(ChangeUserMapper.class);
            if (!CollectionUtils.isEmpty(item)) {
                changeUserMapper.userChangeItemAdd(item, 1);
            }


            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "开阀成功", void.class);
        } catch (Exception e) {
            logger.error("开阀失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "开阀失败", null);
        }

    }

    /**
     * 用户变更事项
     *
     * @param factory
     * @param userInfo
     * @return
     * @throws Exception
     */
    private List<UserChangeItemBean> userChangeItems(SessionFactory factory, ValveCloseDetailBean detailBean, UserInfoBean userInfo, UserBean beforeUserInfo) throws Exception {
        List<UserChangeItemBean> item = new ArrayList<UserChangeItemBean>();
        UserBean afterUserInfo = beforeUserInfo.clone();
        //获取客户、用户字段中英文
        Map<String, String> userFieldTemp = Tools.getFiledName(factory, "user_info");
        Map<String, String> userField = Maps.newHashMap();
        Map<String, String> changeNos = new HashMap<String, String>();
        changeNos.put("userNo", detailBean.getUserNo());
        //changeNos.put("receiptId", bean.getId());
        changeNos.put("receiptType", "VALVE_MANAGEMENT");

        UserBean userBean = new UserBean();
        userBean.setUserNo(detailBean.getUserNo());
        ValveUserMapper valveUserMapper = factory.getMapper(ValveUserMapper.class);
        String valveStatus = valveUserMapper.getValveStatus(detailBean.getUserNo());
        userBean.setValveStatus(Tools.getDicNameByCodeAndValue(factory,"MVS",valveStatus));


        for (String field : userFieldTemp.keySet()) {
            try {
                Field f = UserBean.class.getDeclaredField(field);
                f.setAccessible(true);
                if (f.get(userBean) != null) {
                    userField.put(field, userFieldTemp.get(field));
                    f.set(afterUserInfo, f.get(userBean));
                }
            } catch (NoSuchFieldException e) {
                continue;
            }
        }
        List<UserChangeItemBean> userItem = Tools.getChangeItems(userInfo, changeNos, beforeUserInfo, afterUserInfo, userField);
        item.addAll(userItem);
        return item;
    }

}
